import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  GoodsInfoItem,
  GoodsRelevantListItem,
  AreaList
} from '@/types/data'
export default defineStore('goods', {
  state () {
    return {
      goodsInfo: {} as GoodsInfoItem, // 商品详情数据
      goodsRelevantList: [] as GoodsRelevantListItem[], // 同类推荐数据
      cityList: [] as AreaList[], // 城市数据列表
      initCityList: [] as AreaList[] // 城市列表备份数据
    }
  },
  actions: {
    // 获取商品详情
    async getGoodsInfo (id: string) {
      const res = await request.request<ApiRes<GoodsInfoItem>>({
        url: '/goods',
        params: { id }
      })
      //   console.log(res)
      this.goodsInfo = res.data.result
    },

    // 获取同类推荐数据
    async goodsRelevant (id: string) {
      const res = await request.request<ApiRes<GoodsRelevantListItem[]>>({
        url: '/goods/relevant',
        params: {
          id,
          limit: 16
        }
      })
      // console.log(res)
      this.goodsRelevantList = res.data.result
    },

    // 获取城市数据列表
    async getCityList () {
      const res = await request.request<AreaList[]>({
        url:
          'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
      })
      // console.log(res)
      this.cityList = res.data
      this.initCityList = res.data
    },
    // 设置城市列表数据
    setCityList (item: AreaList[]) {
      this.cityList = item
    },
    // 城市列表数据还原
    setInitCityList () {
      this.cityList = this.initCityList
    }
  }
})
